package com.sr.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaochi
 * @date 2022/4/27 16:38
 * @desc
 */
public class JacksonUtil {

    private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);

    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 对象转json字符串
     * @param obj
     * @param <T>
     * @return
     */
    public static <T>String beanToJsonString(T obj){
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
        }catch (Exception e){
            logger.error("beanToJson error", e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 对象转格式化的json字符串
     * @param obj
     * @param <T>
     * @return
     */
    public static <T>String beanToJsonStringPretty(T obj){
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        }catch (Exception e){
            logger.error("beanToJson error", e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * json字符串转对象
     * @param json
     * @param clazz
     * @return
     */
    public static <T>T jsonToBean(String json,Class<T> clazz) throws Exception {
        if (json == null || clazz == null){
            return null;
        }
        try {
            return objectMapper.readValue(json,clazz);
        }catch (Exception e){
            throw e;
        }
    }

    /**
     * json字符串转对象
     * @param json
     * @param typeReference
     * @return
     * @throws Exception
     */
    public static Object jsonToBean(String json, TypeReference<?> typeReference) throws Exception {
        if (json == null){
            return null;
        }
        try {
            return objectMapper.readValue(json, typeReference.getClass());
        }catch (Exception e){
            throw e;
        }
    }

    /**
     * json字符串转list
     * @param json
     * @param clazz
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> jsonToBeanList(String json, Class<T> clazz){
        if (json == null || clazz == null){
            return null;
        }
        try {
            JavaType type = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
            return objectMapper.readValue(json,type);
        }catch (Exception e){
            logger.error("jsonToBeanList error", e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * json字符串转Map
     * @param json
     * @param clazz
     * @return
     */
    public static <T>Map<String,T> jsonToBeanMap(String json,Class<T> clazz){
        if (json == null || clazz == null){
            return null;
        }
        try {
            JavaType type = objectMapper.getTypeFactory().constructParametricType(Map.class,String.class, clazz);
            return objectMapper.readValue(json,type);
        }catch (Exception e){
            logger.error("jsonToMap error", e);
            e.printStackTrace();
            return null;
        }
    }
}
